//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using C = config;

namespace DataLayerBuilder
{
    internal class Serializer
    {
        public static string EmitSaveParam(C.datatype type, DataGen.FieldDef field,
            Microsoft.Research.DataLayer.Parameter.ParamType paramType, string privName, string pubName)
        {
            switch (type)
            {
                case config.datatype.uri:
                    return (string.Format("            mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, this.{2} == null ? null : this.{2}.ToString()));",
                        field.SQLName, paramType, privName));
                case config.datatype.color:
                    return (string.Format("            mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, this.{2}.ToArgb()));",
                        field.SQLName, paramType, privName));
                case config.datatype.byteArray:
                case config.datatype.image:
                    return (string.Format("            Save{0}Blob(store);", pubName));
                default:
                    return (string.Format("            mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, this.{2}));",
                        field.SQLName, paramType, privName));
            }
        }

        public static string EmitCopyParam(string objName, C.datatype type, DataGen.FieldDef field,
            Microsoft.Research.DataLayer.Parameter.ParamType paramType, string privName, string pubName)
        {
            switch (type)
            {
                case config.datatype.uri:
                    return (string.Format("                 mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, {3}.{2} == null ? null : {3}.{2}.ToString()));",
                        field.SQLName, paramType, privName, objName));
                case config.datatype.color:
                    return (string.Format("                 mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, {3}.{2}.ToArgb()));",
                        field.SQLName, paramType, privName, objName));
                case config.datatype.byteArray:
                case config.datatype.image:
                    return (string.Format("                 {1}.Save{0}Blob(store);", pubName, objName));
                default:
                    return (string.Format("                 mydata.Add(new Parameter(\"{0}\", Parameter.ParamType.{1}, Parameter.ParamDirection.In, {3}.{2}));",
                        field.SQLName, paramType, privName, objName));
            }
        }


        public static StringBuilder EmitLoadCode(C.datatype type)
        {
            StringBuilder fieldLoadSrc;

            switch (type)
            {
                case config.datatype.uri:
                    fieldLoadSrc = new StringBuilder(Properties.Resource1.class_fieldload_uri);
                    break;
                case config.datatype.color:
                    fieldLoadSrc = new StringBuilder(Properties.Resource1.class_fieldload_color);
                    break;
                case config.datatype.image:
                case config.datatype.byteArray:
                    fieldLoadSrc = new StringBuilder();
                    break;
                default:
                    fieldLoadSrc = new StringBuilder(Properties.Resource1.class_fieldload);
                    break;
            }

            return fieldLoadSrc;
        }
    }
}
